#!/bin/sh

set -u
APP=SAMPLE

# CREATE DIRECTORIES AND ADD REMOVER
[ -n "$APP" ] && mkdir -p "/opt/$APP/tmp" "/opt/$APP/icons" && cd "/opt/$APP/tmp" || exit 1
printf "#!/bin/sh\nset -e\nrm -f /usr/local/bin/$APP\nrm -R -f /opt/$APP" > "/opt/$APP/remove"
printf '\n%s' "rm -f /usr/local/share/applications/$APP-AM.desktop" >> "/opt/$APP/remove"
chmod a+x "/opt/$APP/remove"

# DOWNLOADING THE DEPENDENCIES
wget -q "$(curl -Ls https://api.github.com/repos/probonopd/go-appimage/releases | grep -v zsync | grep -i continuous | grep -i appimagetool | grep -i "$(uname -m)" | grep browser_download_url | cut -d '"' -f 4 | head -1)" -O appimagetool
wget https://raw.githubusercontent.com/ivan-hc/AM-application-manager/main/tools/pkg2appimage
chmod a+x ./appimagetool ./pkg2appimage

# CREATING THE APPIMAGE
echo "app: SAMPLE
binpatch: true

ingredients:
  dist: oldstable
  sources:
    - deb http://deb.debian.org/debian/ oldstable main contrib non-free
    - deb http://deb.debian.org/debian-security/ oldstable-security main contrib non-free
    - deb http://deb.debian.org/debian oldstable-updates main contrib non-free
    - deb http://deb.debian.org/debian oldstable-backports main contrib non-free
  packages:
    - SAMPLE" >> recipe.yml

cp /opt/"$APP"/tmp/recipe.yml /opt/"$APP"/recipe.yml

./pkg2appimage ./recipe.yml

# CLEAN METAINFO DIRECTORY
metainfodir=$(find ./"$APP"/"$APP".AppDir -type d -name metainfo | grep "share/metainfo" | head -1)
if [ -z "$metainfodir" ]; then
	return
else
	cd "$metainfodir" || return
	rm -R -f ./*.xml
	cd - > /dev/null || return
fi

# ...EXPORT THE APPDIR TO AN APPIMAGE!
ARCH=$(uname -m) VERSION=$(./appimagetool -v | grep -o '[[:digit:]]*') ./appimagetool -s ./"$APP"/"$APP".AppDir
underscore=_
mkdir version
mv ./"$APP"/"$APP""$underscore"*.deb ./version/
version=$(ls /opt/"$APP"/tmp/version)
echo "$version" >> /opt/"$APP"/version

cd ..
mv ./tmp/*.AppImage ./"$APP"
chmod a+x ./"$APP"

rm -R -f ./tmp

# LINK
ln -s /opt/"$APP"/"$APP" /usr/local/bin/"$APP"

# SCRIPT TO UPDATE THE PROGRAM
cat >> /opt/"$APP"/AM-updater << 'EOF'
#!/bin/sh
APP=SAMPLE
initial=$(echo $APP | head -c 1)
version0=$(cat /opt/"$APP"/version)
url="http://http.us.debian.org/debian/pool/main/$initial/"$APP"/$version0"
if curl --output /dev/null --silent --head --fail "$url"; then
	echo "Update not needed, exit!"
else
	notify-send "A new version of $APP is available, please wait!"
	mkdir /opt/"$APP"/tmp
	cd /opt/"$APP"/tmp || exit 1
	wget -q $(curl -Ls https://api.github.com/repos/probonopd/go-appimage/releases | grep -v zsync | grep -i continuous | grep -i appimagetool | grep -i "$(uname -m)" | grep browser_download_url | cut -d '"' -f 4 | head -1) -O appimagetool
	wget https://raw.githubusercontent.com/ivan-hc/AM-application-manager/main/tools/pkg2appimage # 64 BIT ONLY (comment to disable)
	chmod a+x ./appimagetool ./pkg2appimage
	cp /opt/"$APP"/recipe.yml /opt/"$APP"/tmp/recipe.yml
	./pkg2appimage ./recipe.yml
	metainfodir=$(find ./"$APP"/"$APP".AppDir -type d -name metainfo | grep "share/metainfo" | head -1)
	if [ -z "$metainfodir" ]; then
		return
	else
		cd "$metainfodir" || return
		rm -R -f ./*.xml
		cd - > /dev/null || return
	fi
	ARCH=$(uname -m) VERSION=$(./appimagetool -v | grep -o '[[:digit:]]*') ./appimagetool -s ./"$APP"/"$APP".AppDir
	underscore=_
	mkdir version
	mv ./"$APP"/"$APP""$underscore"*.deb ./version/
	cd ..
	version=$(ls /opt/"$APP"/tmp/version)
	if test -f ./tmp/version/$version; then rm -R -f ./version
	fi
	echo "$version" >> /opt/"$APP"/version
	mv ./tmp/*.AppImage ./"$APP";
	chmod a+x ./"$APP"
	rm -R -f ./tmp
	notify-send "$APP is updated!"
fi
EOF
chmod a+x /opt/"$APP"/AM-updater

# LAUNCHER & ICON
cd "/opt/$APP" || exit 1
./"$APP" --appimage-extract *.desktop 1>/dev/null && mv ./squashfs-root/*.desktop ./"$APP".desktop
./"$APP" --appimage-extract .DirIcon 1>/dev/null && mv ./squashfs-root/.DirIcon ./DirIcon
COUNT=0
while [ "$COUNT" -lt 10 ]; do # Tries to get the actual icon/desktop if it is a symlink to another symlink
	if [ -L ./"$APP".desktop ]; then
		LINKPATH=$(readlink ./"$APP".desktop)
		./"$APP" --appimage-extract "$LINKPATH" && mv ./squashfs-root/"$LINKPATH" ./"$APP".desktop
	fi
	if [ -L ./DirIcon ]; then
		LINKPATH=$(readlink ./DirIcon)
		./"$APP" --appimage-extract "$LINKPATH" && mv ./squashfs-root/"$LINKPATH" ./DirIcon
	fi
	[ ! -L ./"$APP".desktop ] && [ ! -L ./DirIcon ] && break
	COUNT=$((COUNT + 1))
done
sed -i "s#Exec=[^ ]*#Exec=$APP#g; s#Icon=.*#Icon=/opt/$APP/icons/$APP#g" ./"$APP".desktop
mv ./"$APP".desktop /usr/local/share/applications/"$APP"-AM.desktop && mv ./DirIcon ./icons/"$APP" 1>/dev/null
rm -R -f ./squashfs-root
